//
//  LoadView.m
//  工程机械观察
//
//  Created by 毕武侠 on 2016/12/16.
//  Copyright © 2016年 毕武侠. All rights reserved.
//

#import "LoadView.h"
#import <UIImage+GIF.h>

#define kImageViewAnimationKey @"com.zylf.imageViewAnimation"

@interface LoadView ()

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) CAAnimation *animation;
@property (nonatomic, strong) UILabel *text;
@property (nonatomic, strong) HeadTopView *headView;
@property (nonatomic, strong) RACSubject *backSignal;

@end

@implementation LoadView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initConfigureView];
    }
    return self;
}

- (void)initConfigureView {
    self.hidden = YES;
    self.backgroundColor = [UIColor whiteColor];
    
    CGFloat width = self.frame.size.width;
    CGFloat height = self.frame.size.height;
    
    self.backSignal = [RACSubject subject];
    
    // set text
    self.loadingText = @"";
    self.loadNoDataText = @"暂无数据";
    self.loadNoNetText = @"网络链接失败";
    
    self.loadingImage = @"loading_imgBlue";
    self.loadNoDataImage = IMG_EmptyDefault;
    self.loadNoNetImage = IMG_EmptyDefault;
    
    // add imageView
    UIImage *image = [UIImage imageNamed:IMG_EmptyDefault];
    self.imageView.frame = CGRectMake(0, 0, 46, 46);
    self.imageView.center = CGPointMake(width/2, height / 3);
    self.imageView.image = image;
    [self addSubview:self.imageView];
    
    // add text view
    self.text.frame = CGRectMake(0, 0, width - 32, 40);
    self.text.textColor = [UIColor darkGrayColor];
    self.text.center = CGPointMake(width/2, CGRectGetMaxY(self.imageView.frame) + 28);
    [self addSubview:self.text];
}

- (void)loadTopTitle:(NSString *)title {
    self.headView = [[HeadTopView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    self.headView.text = title;
    self.headView.showLeft = YES;
    self.headView.leftImageName = @"返回";
    
    [self addSubview:self.headView];
}

#pragma mark - ViewSignalProtocol

- (RACSignal *)getSignalWithDictionary:(NSDictionary *)dict {
    NSString *str = dict[@"action"];
    if (!str)   return [RACSignal empty];
    
    if ([str isEqualToString:@"back"]) {
        return [self.headView getSignalWithDictionary:@{@"action" : @"left"}];
    } else {
        return [RACSignal empty];
    }
}

// 加载图片
- (UIImageView *)imageView {
    if (_imageView == nil) {
        _imageView = [[UIImageView alloc] init];
    }
    return _imageView;
}

// 文字
- (UILabel *)text {
    if (_text == nil) {
        _text = [[UILabel alloc] init];
        _text.font = [UIFont systemFontOfSize:14];
    }
    return _text;
}

- (CAAnimation *)animation {
    if (_animation == nil) {
        CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform"];
        animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
        animation.toValue = [NSValue valueWithCATransform3D: CATransform3DMakeRotation(M_PI_2, 0.0, 0.0, 1.0) ];
        animation.duration = 0.25;
        animation.cumulative = YES;
        animation.removedOnCompletion = NO;
        animation.fillMode = kCAFillModeForwards;
        animation.repeatCount = MAXFLOAT;
        _animation = animation;
    }
    return _animation;
}

// 显示正在加载页面
- (void)showLoadAnimation {
    self.hidden = NO;
    
    CGFloat width = self.frame.size.width;
    CGFloat height = self.frame.size.height;
    self.imageView.frame = CGRectMake(0, 0, 46, 46);
    self.imageView.center = CGPointMake(width/2, height / 3);
    
//    self.imageView.image = [UIImage imageNamed:self.loadingImage];
//    [self.imageView.layer addAnimation:self.animation forKey:kImageViewAnimationKey];
    
    
#if TGT_ZYWX
    
    NSString *path = [[NSBundle mainBundle] pathForResource:@"loading" ofType:@"gif"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    UIImage *image = [UIImage sd_animatedGIFWithData:data];
    self.imageView.image = image;
    
#elif TGT_XMGK || TGT_XBGK
    self.imageView.image = [UIImage imageNamed:self.loadingImage];
    [self.imageView.layer addAnimation:self.animation forKey:kImageViewAnimationKey];
    
#endif

    
    
    self.text.frame = CGRectMake(0, 0, width - 32, 40);
    self.text.center = CGPointMake(width/2, CGRectGetMaxY(self.imageView.frame) + 28);
    self.text.text = self.loadingText;
}

// 显示没有数据页面
- (void)showLoadNoData {
    self.hidden = NO;
    
    CGFloat width = self.frame.size.width;
    CGFloat height = self.frame.size.height;
    self.imageView.frame = CGRectMake(0, 0, 80, 80);
    self.imageView.center = CGPointMake(width/2, height / 3);
    
    self.imageView.image = [UIImage imageNamed:self.loadNoDataImage];
    [self.imageView.layer removeAnimationForKey:kImageViewAnimationKey];
    
    self.text.frame = CGRectMake(0, 0, width - 32, 40);
    self.text.center = CGPointMake(width/2, CGRectGetMaxY(self.imageView.frame) + 28);
    self.text.text = self.loadNoDataText;
    self.text.textAlignment = NSTextAlignmentCenter;
}

// 显示没有网络页面
- (void)showLoadNoNet {
    self.hidden = NO;
    
    CGFloat width = self.frame.size.width;
    CGFloat height = self.frame.size.height;
    self.imageView.frame = CGRectMake(0, 0, 80, 80);
    self.imageView.center = CGPointMake(width/2, height / 3);
    self.imageView.image = [UIImage imageNamed:self.loadNoNetImage];
    [self.imageView.layer removeAnimationForKey:kImageViewAnimationKey];
    
    self.text.frame = CGRectMake(0, 0, width - 32, 40);
    self.text.center = CGPointMake(width/2, CGRectGetMaxY(self.imageView.frame) + 28);
    self.text.textAlignment = NSTextAlignmentCenter;
    self.text.text = self.loadNoNetText;
    
//    self.text.backgroundColor = [UIColor redColor];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
